package com.fanwh.healthycampus2.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.fanwh.healthycampus2.R;
import com.fanwh.healthycampus2.utils.ThemeManager;
import com.google.android.material.snackbar.Snackbar;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

public class ChangePswActivity extends AppCompatActivity {

    private ImageButton back;
    private TextView title;
    private EditText oldPassword;
    private EditText newPassword;
    private Button success;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置主题色，，，一定要在setView之前
        ThemeManager.getInstance().init(this);

        Bmob.initialize(this, "e85fe1093bc0f95a7ef09f6e23de1cae");
        setContentView(R.layout.activity_change_psw);

        back = findViewById(R.id.ib_navigation_back);
        title = findViewById(R.id.tv_navigation_label);
        oldPassword = findViewById(R.id.old_password);
        newPassword = findViewById(R.id.new_password);
        success = findViewById(R.id.success);

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        title.setText("修改密码");

        success.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String oldpassword = oldPassword.getText().toString();
                String newpassword = newPassword.getText().toString();
                BmobUser.updateCurrentUserPassword(oldpassword, newpassword, new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (e == null) {
                            Toast.makeText(ChangePswActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(ChangePswActivity.this, "修改失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });

    }
}
